package com.lcq.practice.springboot.universal.demo.test_mybatis;

import com.lcq.practice.springboot.universal.demo.commmon.UserPo;
import com.lcq.practice.springboot.universal.demo.test_mybatis.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName MybatisTest
 * @Description
 * @Author lichuanqi
 * @Date 2022/2/11 11:55
 * @Version 1.0
 **/
public class MybatisTest {
    /*依赖*/
    //        <dependency>
    //            <groupId>org.mybatis.spring.boot</groupId>
    //            <artifactId>mybatis-spring-boot-starter</artifactId>
    //            <version>2.2.0</version>
    //        </dependency>
    @Test
    public void test1(){
        String resource = "mybatis-config.xml";

        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try(SqlSession sqlSession = sqlSessionFactory.openSession()){
                UserPo userPo = sqlSession.selectOne("com.lcq.practice.springboot.universal.demo.test_mybatis.mapper.UserMapper.selectUser", "1");
                System.out.println(userPo);
                UserMapper mapper = sqlSession.getMapper(UserMapper.class);
                UserPo userPo2 = mapper.selectUser("2");
                System.out.println(userPo2);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
